/**
 * 
 */
package com.googlecode.pcapj;



/**
 * 
 */
class EthernetPacketImpl extends PacketImpl implements EthernetPacket{
	private MACAddress destination;
	private MACAddress source;
	private PacketFactory<? extends Packet> packetFactory;
	
	/**
	 * @param header
	 * @param buffer
	 * @param offset
	 */
	public EthernetPacketImpl(byte[] buffer, int offset, int length) {
		super(buffer, offset, length);
		
	}
	void setPacketFactory(PacketFactory<? extends Packet> packetFactory) {
		this.packetFactory = packetFactory;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.EthernetPacket#getDestination()
	 */
	public MACAddress getDestination() {
		if(destination == null){
			destination = binaryData.getMACAddress(0);
		}
		return destination;
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.EthernetPacket#getSource()
	 */
	public MACAddress getSource() {
		if(source == null){
			source = binaryData.getMACAddress(6);
		}
		return source;
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.EthernetPacket#getFrameType()
	 */
	public short getFrameType() {
		return binaryData.getShort(12);
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.EthernetPacket#getFrameData()
	 */
	public byte[] getFrameData() {
		return binaryData.getBytes(14, binaryData.getLength() - 14);
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.EthernetPacket#getNetworkLayerPacket()
	 */
	public Packet getNetworkLayerPacket() {
		Packet packet = null;
		if(packetFactory != null){
			packet = packetFactory.newPacket(
					binaryData.getBuffer(), 
					binaryData.getOffset() + 14, 
					binaryData.getLength() - 14);
		}
		return packet;
	}
}
